# myapp/consumers.py
from channels.generic.websocket import WebsocketConsumer
import json
from asgiref.sync import async_to_sync
active_rooms = {}

class ConnectionTestConsumer(WebsocketConsumer):
    i=0
    def connect(self):
        print("-----[Server] 客户端连接成功-----")  # 服务端控制台打印
        self.accept()  
        #异步转同步 加入group
        async_to_sync(self.channel_layer.group_add)("text",self.channel_name)
        #print("group 创建")#
        async_to_sync(self.channel_layer.group_send)(
            "text",
            {
                'type':'enter',
                'data': self.channel_name+' enter'
            }
        )
        # print("发送完成")
    def disconnect(self, close_code):
        print(f"[Server] 连接断开，代码：{close_code}")
        async_to_sync(self.channel_layer.group_discard)("text", self.channel_name)
        self.send(text_data = json.dumps({
                    'type':'leave',
                    'data':'user leave'
                }))
        
   
        # print(event)
    
    def receive(self, text_data=None, bytes_data=None):
        # print("收到消息 1")
        if text_data:
            self.i=self.i+1
            data = json.loads(text_data)
            print(self.channel_name , "json已加载")            
            # print(self.i)
            print("类型:"+data['type'])
            async_to_sync(self.channel_layer.group_send)(
                "text",
                {   
                    'type' : data['type'],
                    "sender" : self.channel_name,
                    "data" : data
                }
            )
            
        if bytes_data:
            print("收到二进制消息",bytes_data)
 # 收到组内消息 

    def text(self,event):
        self.send(text_data=event)
        '''
        
        '''
        
    
    def enter(self,event):
        print("fuc enter :",event)
        self.send(text_data=json.dumps(event))
        '''
        'type',
        [channel_name] enter
        '''
    def candidate(self,event):
        data = event['data']
        if(event['sender'] != self.channel_name):
            #转发candidate
            self.send(text_data= json.dumps(data))
        else:
            msg= {
                'type':'reply',
                'reply':'candidate is arrival'
            }
            self.send(text_data=json.dumps(msg))

    def offer(self,event):
        data = event['data']
        # print(event)
        if(event['sender'] != self.channel_name):
            #转发sdp
            self.send(text_data= json.dumps(data)) 
        else:
            msg= {
                'type':'reply',
                'reply':'offer is arrival'
            } 
            self.send(text_data=json.dumps(msg))
    
    def answer(self,event):
        data = event['data']
        print(data)
        if(event['sender'] != self.channel_name):
            self.send(text_data= json.dumps(data)) 
        else:
            msg= {
                'type':'reply',
                'reply':'answer is arrival'
            } 
            self.send(text_data=json.dumps(msg))
    
        # 
        # 
        # 
        # 
        # 
##############################################################   
class ConnectionChatConsumer(WebsocketConsumer):
    def connect(self):
        room_id = self.scope["url_route"]["kwargs"]["room_id"]
        print(room_id)
        print("ChatConsumer 已连接")

    def disconnect(self , code):
        print("ChatConsumer 已断开连接")

    def receive(self, text_data=None, bytes_data=None):
        print()        

